Repository navigation
Insert through UseClickHouse(DbConnection) and follow runtime connection changes - #70
Draft
alex-clickhouse wants to merge 2 commits into
Conversation
GetClickHouseClient() threw unless the context had a ClickHouseDataSource, so SaveChanges and BulkInsertAsync failed with UseClickHouse(DbConnection). For a ClickHouseConnection, the relational connection now creates and keeps a ClickHouseClient from a copy of the connection settings, with the database of the connection, and disposes it with the context. The data source from the options was never cleared, so after SetConnectionString or SetDbConnection, inserts and the master connection used the original target, and a SetConnectionString before the first query (as dotnet ef --connection does) had no effect. The ConnectionString setter now gets the provider-owned data source for the new string, and SetDbConnection with a connection clears the data source. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review found two cases where inserts used _dataSource although the live connection had another target: ChangeDatabase followed by SetConnectionString (the driver keeps the selected database), and SetConnectionString with the string the connection already has (EF does not change the connection, so it keeps its code-only settings). A ChangeDatabase on a connection from a data source had the same problem. The data source client and settings are now used only for the connection that CreateDbConnection got from the data source, while it has the data source database. Other ClickHouseConnections get the copied-settings client. A connection of another type keeps the data source client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two defects in
ClickHouseRelationalConnection, which share the same state:UseClickHouse(DbConnection)cannot write.GetClickHouseClient()throws unless the context has aClickHouseDataSource, soSaveChangesandBulkInsertAsyncalways fail on this path._dataSourceis set in the constructor and never cleared. AfterSetConnectionString(...)orSetDbConnection(...), queries go to the new target, but inserts and the master connection (EnsureCreated,EnsureDeleted, migrations) go to the original one. On the connection-string path, aSetConnectionStringbefore the first query has no effect. Because of this,dotnet ef database update --connection "..."applies migrations to the context's own connection string.Server-free tests reproduced each case on the old code before the fix. Three more cases came up during the work, and are fixed too:
ChangeDatabaseon a connection from a data source sent inserts to the data source database.ChangeDatabasefollowed bySetConnectionStringsplit queries and inserts across databases.SetConnectionStringwith the unchanged string dropped code-only settings, such asBearerToken, from inserts.Solution
Rule: inserts and the master connection use the same target as the current connection.
ClickHouseConnection: the driver keeps the connection's client internal, soClickHouseRelationalConnectionowns a client made from a copy of the connection settings, withDatabase = connection.Database(this includesChangeDatabase). The client is made again when the settings object or the database changes. It is disposed onDispose, onSetDbConnectionand on a connection-string change, and kept across a pooled-context reset.ConnectionStringsetter: gets the provider-owned data source for the new string fromClickHouseDataSourceManager.GetOrCreateDataSource(now public). This removes a data source fromUseClickHouse(DbDataSource), as Npgsql does.SetDbConnectionwith a connection: clears the data source, as Npgsql does.ClickHouseDataSourcestill throws, and the message now gives the connection type.Decisions for the reviewer
SetDbConnection(null)keeps the configured data source, so the next connection comes from it again.SetConnectionString, not at first use.BulkInsertAsyncnow creates the connection object, so connection interceptors see it.SetConnectionStringon a connection that already exists, inserts use one more client (and HTTP pool), because EF Core writes the new string into the existing connection.SetConnectionStringgets a cached data source in the singletonClickHouseDataSourceManager, as each distinct string inUseClickHouse(string)does already. An application that switches between many tenant strings keeps one data source for each. Npgsql uses a plain connection afterSetConnectionStringinstead.Not changed: named connection strings (
Name=...) are not resolved in the constructor, and theEnsureCreateddatabase name ignoresChangeDatabase.Tests
ConnectionTargetTests, 24 tests that need no server. They failed on the old code and pass now.ConnectionWriteTargetTests(SaveChangesandBulkInsertAsyncthrough aClickHouseConnection, each swap case,ChangeDatabase,EnsureCreatedafter a swap, master connection target) andDotnetEfCliTests.Database_update_uses_connection_option. Docker was not available, so these integration tests did not run. CI will run them after the base changes tomain.ConnectionSettingsTests.Docs
Entries in the v0.3.1 "Bug fixes" sections of
CHANGELOG.mdandRELEASENOTES.md, and a paragraph inREADME.mdon how inserts choose their client and target.🤖 Generated with Claude Code